<?php

// Because static constructor methods can't be in the class in php5.
// (http://php.benscom.com/manual/en/language.oop5.static.php#51627)
//Recommendation::$auth = new UserAccount;

class Recommendation {

	//public static $auth; // Initialized outside.  (see above)

	//public $comment

	function __construct() {
		//$this->auth = new UserAccount;
		$this->error_str = '';
	}

	// Get Recommendation array.
	public static function GetRecArray($initStr, $pageLength, $movePages, $commentCount, &$recCount) {
		global $dbobj;

		$sql = "SELECT rec.*, usr.userIconPath ";
		if ($commentCount) {
			$sql .= ", (SELECT COUNT(*) FROM mr_recComment cmt WHERE cmt.recID = rec.recID AND cmt.disabled = 0) AS comment_count ";
		}
		if (strpos($initStr, "all") === 0) {
			$sql .= "FROM mr_recommendation rec " .
				"INNER JOIN mr_useraccount usr ON rec.username = usr.username " .
				"WHERE rec.disabled = 0 ";
		}
		elseif (strpos($initStr, "user_out:") === 0) {
			$username = array_pop(explode(":", $initStr));
			$sql .= "FROM mr_recommendation rec " .
				"INNER JOIN mr_useraccount usr ON rec.username = usr.username " .
				"WHERE rec.username = " . ValToSQL_str($username) . " AND rec.disabled = 0 ";
		}
		elseif (strpos($initStr, "user_in:") === 0) {
			$username = array_pop(explode(":", $initStr));
			$sql .= "FROM mr_recommendation rec " .
				"INNER JOIN mr_useraccount usr ON rec.username = usr.username " .
				"INNER JOIN mr_subscription sub ON rec.username = sub.subscribedToUsername " .
				"WHERE sub.username = " . ValToSQL_str($username) . " AND rec.disabled = 0 ";
		}
		else {
			echo "<p>Error: Recommendation::GetRecArray(): initStr invalid.";
			exit();
		}
		$sql .= "ORDER BY recID DESC";

		$rec_ar = $dbobj->GetNestedArrayExt($sql, $pageLength, $pageLength*$movePages, $recCount);
		return $rec_ar;
	}


	// Get Recommendation string from artist, album, song.
	public static function GetRecText($artist, $album, $song) {
		$rec_text = trim($artist);
		if ($album != "")
			$rec_text .= " - " . trim($album);
		if ($song != "")
			$rec_text .= " - " . trim($song);
		return $rec_text;
	}


	public function InitFromFieldArray($comment) {
		$this->rec = $comment;
	}

	// Maybe this function wants to be static...?
	public function InitFromKey($recID) {
		global $dbobj;
		
		$rec_ar = $dbobj->GetResultsAsNestedArray("SELECT * FROM mr_recommendation WHERE recID = " . $recID . "", 1);
		$update_r = $rec_ar[0];
		$this->rec = $update_r;
	}


	public function isUpdatingAllowed() {
	
		if ($this->rec['adminApproved']) {
			$this->error_str = "This update is permanently locked.";
			return false;
		}

		if (!$this->rec['allowUpdatesFromIP']) {
			$this->error_str = "This update does not allow updating.";
			return false;
		}
		elseif ($this->rec['ipAddedBy'] !== $_SERVER['REMOTE_ADDR']) {
			$this->error_str = "This update does not allow updating from here.";
			return false;
		}

		// If there were no blockers, say it's "ok".
		return true;
	}


	public function isCommentLocked() {
	
		if ($this->rec['adminApproved']) {
			//$this->error_str = "This comment is permanently locked.";
			return true;
		}

		return false;
	}
	
	

	public function RemoveRecommendation() {
		global $dbobj;

		$dbobj->SendQuery("UPDATE mr_recommendation SET disabled = 1 WHERE recID = " . $this->rec['recID'] . "");
	
		return true;
	}


	public static function DescriptionHtml($entry_text) {
		require_once('inc_texttohtml.php');

		return TextToHtml::GetHtml($entry_text, false, false);
	}

}

?>